<?php
namespace Mentors\Controller;

class ProfileController extends MainController
{
    public function index()
    {
        $account = M('Member')->where('userid=' . $this->userID)->find();
        $this->assign('account', $account);
        $user = M('Mentors')->where('userid=' . $this->userID)->find();
        if ($user) {
            $phone = $user['phone'];
        } else {
            $phone = $account['phone'];
        }
        $this->assign('phone', $phone);
        $map['show'] = 1;
        $train = M('TrainCate')->where($map)->order('sort asc')->select();
        $this->assign('train', $train);
        $state = M('States')->select();
        $this->assign('state', $state);
        $nowYear = date('Y') - 10;
        $this->assign('nowYear', $nowYear);
        $this->display();
    }
    public function save()
    {
        $data['trainItem'] = I('post.trainItem');
        if ($data['trainItem'] == '') {
            $state = 'Please choose the training program';
            echo $this->echo_json_str($state, $message, $url);
            die;
        } else {
            $data['trainItem'] = '-' . implode('-', $data['trainItem']) . '-';
        }
        if (!empty($_FILES['file']['tmp_name'])) {
            $file = $this->_upload();
            if ($file['state'] == "success") {
                $data['resume'] = $file['resume'];
                $data['document'] = $file['name'];
            } else {
                echo $this->echo_json_str($file['msg']);
                die;
            }
        }
        $data['nickname'] = I('post.nickname');
        $data['firstName'] = I('post.firstName');
        $data['lastName'] = I('post.lastName');
        $data['gender'] = I('post.gender');
        $data['birthday'] = I('post.birthday');
        $data['address'] = I('post.address');
        $data['city'] = I('post.city');
        $data['state'] = I('post.state');
        $data['zip'] = I('post.zip');
        $data['exp'] = I('post.exp');
        $data['phone'] = I('post.phone');
        $data['skill'] = I('post.skill');
        $data['working'] = I('post.working');
        $data['expertise'] = I('post.expertise');
        $data['position'] = I('post.position');
        $data['description'] = I('post.description');
        $data['updateTime'] = time();
        unset($map);

        if ($data['nickname']!='') {
            $map['nickname'] = $data['nickname'];
            $map['userid'] = array('neq', $this->userID);
            $r = M('Mentors')->where($map)->find();
            if ($r) {
                echo $this->echo_json_str('Validation of the nickname be repeated by other profile nickname', $message, $url);
                die;
            }
        }
        $obj = M('Mentors');
        unset($map);
        $map['userid'] = $this->userID;
        $list = $obj->where($map)->find();
        if ($list) {
            $obj->where($map)->save($data);
        } else {
            $data['userid'] = $this->userID;
            $data['createTime'] = time();
            $obj->add($data);
        }
        $state = 'SUCCESS';
        $message = 'operation success';
        $url = U('Index/index');
        echo $this->echo_json_str($state, $message, $url);
    }
    //编辑密码
    public function setpsd()
    {
        if ($_POST) {
            $oldPwd = I("oldpassword");
            $password = I("password");
            $map['userid'] = $this->userID;
            $list = M("Member")->where($map)->find();
            if ($list['password'] != md5($oldPwd)) {
                echo $this->echo_json_str("Old password input error");
                exit;
            } else {
                if (M("Member")->where($map)->setField("password", md5($password))) {
                    cookie(null);
                    echo $this->echo_json_str("SUCCESS", L("_action_success"), U("Home/Login/index"));
                    exit;
                } else {
                    echo $this->echo_json_str("Your new password can't be your old password");
                    exit;
                }
            }
        } else {
            $this->display();
        }
    }
    #文件上传
    private function _upload()
    {
        $path = '.' . C('UPLOAD_PATH') . 'resume/';
        //import("ORG.Net.UploadFile");
        $upload = new \Think\Upload();
        // 实例化上传类
        //$upload=new UploadFile();
        $upload->maxSize = 1000000;
        //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
        $upload->rootPath = $path;
        //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
        $upload->saveRule = uniqid;
        //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
        $upload->uploadReplace = true;
        //如果存在同名文件是否进行覆盖
        $upload->exts = array('doc', 'docx', 'pdf');
        //准许上传的文件后缀
        //$upload->allowTypes= array('application/vnd.ms-excel'); //检测mime类型
        $info = $upload->upload();
        if ($info) {
            foreach ($info as $file) {
                if (empty($dir)) {
                    $url = $file['savepath'] . $file['savename'];
                } else {
                    $url = $dir . '/' . $file['savepath'] . $file['savename'];
                }
                $name = $file['name'];
            }
            return array('state' => "success", 'name' => $name, 'resume' => C('UPLOAD_PATH') . 'resume/' . $url);
        } else {
            //是专门来获取上传的错误信息的
            return array('state' => "error", 'msg' => $upload->getError());
        }
    }
}